package cn.qy.kotlin.skill

data class Department(
    var current: Int?,
    var parent: Int?
)

fun main() {
    var departments = listOf<Department>(Department(4, 3),
        Department(3, 2),
        Department(2, 1))
    println("departments : $departments")

    var input = 4
    val expect = 0
    var output:Int?
    do {
        output = find(input, departments)
        if (output == null) {
            break;
        }
        if (expect == output) {
            println("我找到了$output")
            break;
        }
        input = output
    } while (true)

}

private fun find(input: Int, departments: List<Department>): Int? {
    var output: Int? = null
    // 查询序号 4 的父部门是否是 1
    for (department in departments) {
        if (department.current == input) {
            output = department.parent
            println("部门${department.current}的父部门是${department.parent}")
            break
        }
    }
    return output
}